package com.manner.groupon.exception;

import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.ErrorCode;
import com.manner.groupon.common.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * @author manner
 * @description 全局异常处理
 * @createDate 2024-10-10 16:28:50
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(CustomException.class)
    public BaseResponse fail(CustomException customException) {
        log.info("[业务异常，异常原因为]: {}", customException.getMessage());
        return ResultUtils.fail(customException.getMessage());
    }

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public BaseResponse<String> uploadMax(MaxUploadSizeExceededException exceededException) {
        log.error("文件上传过大");
        return ResultUtils.fail("文件大小超出可允许大小");
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException:", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR, "系统错误");
    }
}
